2020-2021 Sunseeker Telemetry and Lighting System
eusci_b_spi_ex1_master.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 #include "driverlib.h"
33 
34 //*****************************************************************************
50 // MSP430F67791A
77 //*****************************************************************************
78 
79 uint8_t RXData = 0;
80 uint8_t TXData = 0;
81 
82 void main(void)
83 {
84  volatile uint16_t i;
85 
86  //Stop watchdog timer
87  WDT_A_hold(WDT_A_BASE);
88 
89  //Configure Pins for UCB0CLK, UCB0SIMO, UCB0SOMI
90  //Set P2.5, P2.6, P2.7 as Module Function Input.
91  GPIO_setAsPeripheralModuleFunctionInputPin(
92  GPIO_PORT_P2,
93  GPIO_PIN5 + GPIO_PIN6 + GPIO_PIN7
94  );
95 
96  //Set the XT1 frequency to UCS
97  UCS_setExternalClockSource(32768, 0);
98 
99  //Initialize Master
100  EUSCI_B_SPI_initMasterParam param = {0};
101  param.selectClockSource = EUSCI_B_SPI_CLOCKSOURCE_SMCLK;
102  param.clockSourceFrequency = UCS_getSMCLK();
103  param.desiredSpiClock = 500000;
104  param.msbFirst = EUSCI_B_SPI_MSB_FIRST;
105  param.clockPhase = EUSCI_B_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT;
106  param.clockPolarity = EUSCI_B_SPI_CLOCKPOLARITY_INACTIVITY_HIGH;
107  param.spiMode = EUSCI_B_SPI_3PIN;
108  EUSCI_B_SPI_initMaster(EUSCI_B0_BASE, &param);
109 
110  //Enable SPI module
111  EUSCI_B_SPI_enable(EUSCI_B0_BASE);
112 
113  EUSCI_B_SPI_clearInterrupt(EUSCI_B0_BASE,
114  EUSCI_B_SPI_RECEIVE_INTERRUPT);
115  // Enable USCI_B0 RX interrupt
116  EUSCI_B_SPI_enableInterrupt(EUSCI_B0_BASE,
117  EUSCI_B_SPI_RECEIVE_INTERRUPT);
118 
119  //Wait for slave to initialize
120  __delay_cycles(100);
121 
122  TXData = 0x1; // Holds TX data
123 
124  //USCI_B0 TX buffer ready?
125  while (!EUSCI_B_SPI_getInterruptStatus(EUSCI_B0_BASE,
126  EUSCI_B_SPI_TRANSMIT_INTERRUPT)) ;
127 
128  //Transmit Data to slave
129  EUSCI_B_SPI_transmitData(EUSCI_B0_BASE, TXData);
130 
131  __bis_SR_register(LPM0_bits + GIE); // CPU off, enable interrupts
132  __no_operation(); // Remain in LPM0
133 }
134 
135 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
136 #pragma vector=USCI_B0_VECTOR
137 __interrupt
138 #elif defined(__GNUC__)
139 __attribute__((interrupt(USCI_B0_VECTOR)))
140 #endif
141 void USCI_B0_ISR (void)
142 {
143  switch (__even_in_range(UCB0IV, USCI_SPI_UCTXIFG))
144  {
145  case USCI_SPI_UCRXIFG: // UCRXIFG
146  //USCI_B0 TX buffer ready?
147  while (!EUSCI_B_SPI_getInterruptStatus(EUSCI_B0_BASE,
148  EUSCI_B_SPI_TRANSMIT_INTERRUPT));
149 
150  RXData = EUSCI_B_SPI_receiveData(EUSCI_B0_BASE);
151 
152  //Increment data
153  TXData++;
154 
155  //Send next value
156  EUSCI_B_SPI_transmitData(EUSCI_B0_BASE,
157  TXData
158  );
159 
160  //Delay between transmissions for slave to process information
161  __delay_cycles(40);
162  break;
163  default:
164  break;
165  }
166 }
167 
uint8_t RXData
void main(void)
void USCI_B0_ISR(void)
uint8_t TXData
MPU_initThreeSegmentsParam param
__no_operation()
__delay_cycles(500000)